package com.zf.rabbitmq.producer.direct;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * direct 类型交换机，
 * 使用 AmqpAdmin API声明
 */
@Component
public class DirectExchangeConfig implements InitializingBean {
    public static final String testDirectExchangeName = "test_direct_exchange";
    public static final String testDirectQueueName = "test_direct_queue";
    public static final String testDirectRouteKey = "test_direct_route_key";

    @Autowired
    private AmqpAdmin amqpAdmin;


    @Override
    public void afterPropertiesSet() throws Exception {
        // 声明交换机
        Exchange exchange = ExchangeBuilder.directExchange(testDirectExchangeName).durable(true).build();
        amqpAdmin.declareExchange(exchange);

        // 声明队列
        Queue queue = QueueBuilder.durable(testDirectQueueName).build();
        amqpAdmin.declareQueue(queue);

        // 声明绑定
        Binding binding = BindingBuilder.bind(queue).to(exchange).with(testDirectRouteKey).noargs();
        amqpAdmin.declareBinding(binding);
    }
}
